﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption

Partial Class renreport
    Inherits System.Web.UI.Page

    Private Sub LoadREN()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT Name,StaffId FROM AW_STAFF ORDER BY Name"
        objDB.FillData(TempDT, "Staff", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        ddlREN.DataSource = TempDT
        ddlREN.DataTextField = "Name"
        ddlREN.DataValueField = "StaffId"
        ddlREN.DataBind()

        'Begin Selecting Currrent User as REN if default REN is selected.
        If Session.Item("StaffType").ToString = "2" Then

            For Each objListItem As ListItem In ddlREN.Items
                If objListItem.Value.ToString = Session.Item("StaffId").ToString Then
                    objListItem.Selected = True
                    Exit For
                End If
            Next
            pnlREN.Visible = True
        Else
            pnlREN.Visible = True
            ddlREN.Items.Add("ALL")
        End If

    End Sub

    Private Sub LoadMonthSel()

        Dim dtMonth As New Data.DataTable
        dtMonth.Columns.Add("Month")
        dtMonth.Columns.Add("MonthValue")

        Dim drNewRow As Data.DataRow

        For nLoop As Integer = 1 To 12

            Select Case nLoop

                Case 1
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "January"
                    drNewRow.Item("MonthValue") = 1
                    dtMonth.Rows.Add(drNewRow)
                Case 2
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "February"
                    drNewRow.Item("MonthValue") = 2
                    dtMonth.Rows.Add(drNewRow)
                Case 3
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "March"
                    drNewRow.Item("MonthValue") = 3
                    dtMonth.Rows.Add(drNewRow)
                Case 4
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "April"
                    drNewRow.Item("MonthValue") = 4
                    dtMonth.Rows.Add(drNewRow)
                Case 5
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "May"
                    drNewRow.Item("MonthValue") = 5
                    dtMonth.Rows.Add(drNewRow)
                Case 6
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "June"
                    drNewRow.Item("MonthValue") = 6
                    dtMonth.Rows.Add(drNewRow)
                Case 7
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "July"
                    drNewRow.Item("MonthValue") = 7
                    dtMonth.Rows.Add(drNewRow)
                Case 8
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "August"
                    drNewRow.Item("MonthValue") = 8
                    dtMonth.Rows.Add(drNewRow)
                Case 9
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "September"
                    drNewRow.Item("MonthValue") = 9
                    dtMonth.Rows.Add(drNewRow)
                Case 10
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "October"
                    drNewRow.Item("MonthValue") = 10
                    dtMonth.Rows.Add(drNewRow)
                Case 11
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "November"
                    drNewRow.Item("MonthValue") = 11
                    dtMonth.Rows.Add(drNewRow)
                Case 12
                    drNewRow = dtMonth.NewRow
                    drNewRow.Item("Month") = "December"
                    drNewRow.Item("MonthValue") = 12
                    dtMonth.Rows.Add(drNewRow)
            End Select
        Next

        ddlMonthSel.DataSource = dtMonth
        ddlMonthSel.DataTextField = "Month"
        ddlMonthSel.DataValueField = "MonthValue"
        ddlMonthSel.DataBind()

        ddlMonthSel.SelectedValue = Now.Month
    End Sub

    Private Sub LoadYear()

        Dim YearDT As New Data.DataTable
        YearDT.Columns.Add("Year")

        Dim NewYearRow As Data.DataRow
        For nLoop As Integer = 7 To 1 Step -1
            NewYearRow = YearDT.NewRow
            NewYearRow.Item("Year") = Now.AddYears(-nLoop).Year
            YearDT.Rows.Add(NewYearRow)
        Next

        NewYearRow = YearDT.NewRow
        NewYearRow.Item("Year") = Now.Year
        YearDT.Rows.Add(NewYearRow)

        For nLoop As Integer = 1 To 7
            NewYearRow = YearDT.NewRow
            NewYearRow.Item("Year") = Now.AddYears(nLoop).Year
            YearDT.Rows.Add(NewYearRow)
        Next

        ddlYearSel.DataSource = YearDT
        ddlYearSel.DataTextField = "Year"
        ddlYearSel.DataValueField = "Year"
        ddlYearSel.DataBind()

        ddlYearSel.SelectedValue = Now.Year
    End Sub

    Private Sub LoadCalendar(ByVal CurrentMonth As Integer, ByVal CurrenYear As Integer)

        Dim strBuilder As New StringBuilder

        strBuilder.AppendLine("<table id=""myTable"" cellpadding=""0"" cellspacing=""0"" border=""0"" bordercolor=""#c2c2c2"" width=""100%"" class=""Calendar-Table"" >")
        'Building the Day Row
        strBuilder.AppendLine("<thead><tr>")
        strBuilder.AppendLine("<th class=""day"">Sunday</th>")
        strBuilder.AppendLine("<th class=""day"">Monday</th>")
        strBuilder.AppendLine("<th class=""day"">Tuesday</th>")
        strBuilder.AppendLine("<th class=""day"">Wednesday</th>")
        strBuilder.AppendLine("<th class=""day"">Thursday</th>")
        strBuilder.AppendLine("<th class=""day"">Friday</th>")
        strBuilder.AppendLine("<th class=""day"">Saturday</th>")
        strBuilder.AppendLine("</tr></thead>")
        'End Building Day Row

        'Getting First Date and LastDate of the Month
        Dim DateStart As Integer = 1
        Dim DateEnd As Integer = CInt(DateTime.ParseExact("1/" & CurrentMonth & "/" & CurrenYear, "d/M/yyyy", Nothing).AddMonths(1).AddDays(-1).ToString("dd"))

        'Begin Reading Database
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim CalEntryDt As New Data.DataTable
        Dim EntryDV As Data.DataView

        objDB.OpenConnection()
        If ddlREN.SelectedItem.Text <> "ALL" Then
            objDB.dbVariable.SQLString = "SELECT RR.RenReportId,IsNull(Cast(RR.Title as varchar(max)),'') +' ' + IsNull(Cast(RC.Name as varchar(max)),'') + ';' + IsNull(Cast(RC.Mobile as varchar(max)),'') + ';' + IsNull(Cast(RC.Phone as varchar(max)),'') as Title,RR.RenReportType,RR.RenDate,RR.RenMonth,RR.RenYear FROM AW_RenReport RR " & _
                "LEFT JOIN AW_RenContact RC ON RR.RenContactId = RC.RenContactId " & _
                "WHERE RR.RenMonth=@RenMonth AND RR.RenYear=@RenYear AND RR.CreateBy=@CreateBy AND RR.StatusDelete=0 "
            objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(ddlREN.SelectedValue.ToString))
        Else
            objDB.dbVariable.SQLString = "SELECT RR.RenReportId, '(' + STAFF.Name + ')' + IsNull(Cast(RR.Title as varchar(max)),'') +' ' + IsNull(Cast(RC.Name as varchar(max)),'') + ';' + IsNull(Cast(RC.Mobile as varchar(max)),'') + ';' + IsNull(Cast(RC.Phone as varchar(max)),'') as Title,RR.RenReportType,RR.RenDate,RR.RenMonth,RR.RenYear FROM AW_RenReport RR " & _
                "LEFT JOIN AW_RenContact RC ON RR.RenContactId = RC.RenContactId " & _
                "LEFT JOIN AW_STAFF STAFF ON STAFF.StaffId = RR.CreateBy " & _
                "WHERE RR.RenMonth=@RenMonth AND RR.RenYear=@RenYear AND RR.StatusDelete=0 "
        End If

        objDB.AddParameter("RenMonth", Data.SqlDbType.TinyInt, CurrentMonth)
        objDB.AddParameter("RenYear", Data.SqlDbType.Int, CurrenYear)
        'objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(Session.Item("StaffId").ToString))
        'Changed by : Daniel Lee
        'Date : 26-July-2011
        'Purpose : To change to support BOSS view and REN view where REN only can see his entry while BOSS has the option to select which REN

        objDB.FillData(CalEntryDt, "CalendarEntry", Database.SQLCommandType.NormalString)
        objDB.CloseConnection()

        EntryDV = New Data.DataView(CalEntryDt)

        'End Getting First Date

        'Begin Populating the Date
        Dim CurrentDay As Date
        Dim WeekLoop As Integer = 0

        For nLoop As Integer = DateStart To DateEnd

            If WeekLoop = 0 Then
                strBuilder.AppendLine("<tr>")
                '    strBuilder.AppendLine("<td class=""date"">")
                'Else
                '    strBuilder.AppendLine("<td class=""date"">")
            End If


            CurrentDay = DateTime.ParseExact(nLoop & "/" & CurrentMonth & "/" & CurrenYear, "d/M/yyyy", Nothing)

            Dim CurrWeekLoop As Integer = WeekLoop
            For innerNloop As Integer = CurrWeekLoop To 6
                WeekLoop = innerNloop
                If CInt(CurrentDay.DayOfWeek) = innerNloop Then
                    strBuilder.AppendLine("<td class=""date"">")
                    'if the currenday is the day of the week then
                    'Begin Appending the Date and the Item in the Calendar
                    strBuilder.AppendLine("<table class=""Grid-Calendar"" width=""100%"">")
                    'Date Row
                    strBuilder.AppendLine("<thead><tr>")
                    strBuilder.AppendLine("<th class=""dateNewButton"" align=""left"">")
                    strBuilder.AppendLine("<a href=""RenReportEntry.aspx?date=" & SymmetricEncryptToHex(nLoop & "/" & CurrentMonth & "/" & CurrenYear, "augment") & """><img src=""images/style01/AddNewButton-Entry.png"" border=""0"" /></a><img src=""images/style01/search-box-divider.gif"" width=""2"" height=""20"" class=""search-divider"" />")
                    strBuilder.AppendLine("</th>")
                    strBuilder.AppendLine("<th class=""dateValue"" style=""text-align:center"" width=""25px"">")
                    strBuilder.AppendLine(nLoop)
                    strBuilder.AppendLine("</th>")
                    strBuilder.AppendLine("</tr></thead>")
                    'End Date Row

                    'Entry Row

                    EntryDV.RowFilter = "RenDate=" & nLoop & " AND RenMonth=" & CurrentMonth & " AND RenYear=" & CurrenYear
                    strBuilder.AppendLine("<tbody><tr>")
                    strBuilder.AppendLine("<td class=""dateentry"" colspan=""2"">")
                    If EntryDV.Count > 0 Then

                        If EntryDV.Count > 5 Then
                            For nLoop2 As Integer = 0 To 4
                                strBuilder.AppendLine("<a href=""RenReportEntry.aspx?id=" & SymmetricEncryptToHex(EntryDV(nLoop2).Item("RenReportId").ToString, "augment") & """>" & EntryDV(nLoop2).Item("Title").ToString & "</a><br >")
                            Next
                        Else
                            For nLoop2 As Integer = 0 To EntryDV.Count - 1
                                strBuilder.AppendLine("<a href=""RenReportEntry.aspx?id=" & SymmetricEncryptToHex(EntryDV(nLoop2).Item("RenReportId").ToString, "augment") & """>" & EntryDV(nLoop2).Item("Title").ToString & "</a><br >")
                            Next
                        End If
                    Else
                        strBuilder.AppendLine("&nbsp")
                    End If
                    'strBuilder.AppendLine("<a href=""http://goo.gl/ayrSC"">THIS IS WHERE YOUR ENTRY IS</a>")
                    strBuilder.AppendLine("</td>")
                    strBuilder.AppendLine("</tr></tbody>")
                    'End Entry Row

                    'Footer Row
                    strBuilder.AppendLine("<tfoot><tr>")
                    strBuilder.AppendLine("<td class=""datefooter"" align=""right"" colspan=""2"" >")
                    If EntryDV.Count > 5 Then
                        'Edit by tanchinteong 20130904
                        '    strBuilder.AppendLine("<a style=""border:0"" href=""RenReportMore.aspx?date=" & SymmetricEncryptToHex(nLoop & "/" & CurrentMonth & "/" & CurrenYear, "augment") & """><img src=""images/style01/Btn-more.png"" /></a>")
                        strBuilder.AppendLine("&nbsp;")
                    Else
                        strBuilder.AppendLine("&nbsp;")
                    End If
                    strBuilder.AppendLine("</td>")
                    strBuilder.AppendLine("</tr></tfoot>")
                    'End Footer Row

                    strBuilder.AppendLine("</table>")
                    Exit For
                Else
                    If innerNloop = 6 Then
                        strBuilder.AppendLine("<td class=""date"" style=""border-width:0px"">")
                        strBuilder.AppendLine("&nbsp;")
                    Else
                        strBuilder.AppendLine("<td class=""date"" style=""border-width:0px"">")
                        strBuilder.AppendLine("&nbsp;</td>")
                    End If

                End If
            Next

            If WeekLoop = 6 Then
                WeekLoop = 0
                strBuilder.AppendLine("</td>")
                strBuilder.AppendLine("</tr>")
            Else
                WeekLoop = WeekLoop + 1
                strBuilder.AppendLine("</td>")
            End If
        Next

        strBuilder.AppendLine("</table>")
        litCalendar.Text = strBuilder.ToString

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        If Not Page.IsPostBack Then
            LoadMonthSel()
            LoadYear()
            LoadREN()
            LoadCalendar(ddlMonthSel.SelectedValue, ddlYearSel.SelectedValue)
            If Session.Item("StaffType").ToString = "2" Then
                pnlREN.Visible = False
            Else
                pnlREN.Visible = True
            End If

        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '0-Back Page
            '1-Month
            '2-Year
            '3-Name
            If arr(0).ToString.Contains("renreport.aspx") Then
                ddlMonthSel.SelectedValue = arr(1).ToString
                ddlYearSel.SelectedValue = arr(2).ToString
                ddlREN.SelectedValue = arr(3).ToString
                Session.Remove("Back")
                LoadCalendar(ddlMonthSel.SelectedValue, ddlYearSel.SelectedValue)
            Else
                Session.Remove("Back")
            End If
        End If
    End Sub

    Protected Sub imgSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSearch.Click
        LoadCalendar(ddlMonthSel.SelectedValue, ddlYearSel.SelectedValue)
        'Dim strBack As String
        'strBack = "renreport.aspx" & "ڡ" & ddlMonthSel.SelectedValue & "ڡ" & ddlYearSel.SelectedValue & "ڡ" & ddlREN.SelectedValue
        'Session.Add("Back", strBack)
    End Sub

    Protected Sub ddlREN_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlREN.SelectedIndexChanged
        LoadCalendar(ddlMonthSel.SelectedValue, ddlYearSel.SelectedValue)
        'Dim strBack As String
        'strBack = "renreport.aspx" & "ڡ" & ddlMonthSel.SelectedValue & "ڡ" & ddlYearSel.SelectedValue & "ڡ" & ddlREN.SelectedValue
        'Session.Add("Back", strBack)
    End Sub
End Class
